package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约服务实现类
 *
 * @author ：zengjie
 * @date ：2020/11/11
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 批量上传预约
     */
    @Override
    public void batchOrderSetting(List<OrderSetting> oslist) {
        //非空判断
        if (oslist != null && oslist.size() > 0) {
            //循环遍历集合
            for (OrderSetting orderSetting : oslist) {
                //先查询是否数据库存在对应时间的预约数据
                int cont = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                //如果有,就根据时间修改对应数据
                if (cont > 0) {
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    //否则就新增
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    /**
     * 查询日历中的预约信息,回显
     * @param yearMonth
     * @return
     */
    @Override
    public List<Map> findOSByYearMonth(String yearMonth) {
        //1.拼接查询条件
        String startDate = yearMonth+"-01";
        String endDate = yearMonth+"-31";
        //2根据起始时间和结束时间获取预约设置数据
        List<OrderSetting> listOS = orderSettingDao.findOSByYearMonth(startDate,endDate);
        //定义返回结果对象
        List<Map> mapList = new ArrayList<>();
        //3将List<OrderSeting>转 List<Map>
        if(listOS != null && listOS.size()>0){
            for (OrderSetting os : listOS) {
                Map rsMap = new HashMap();
                rsMap.put("date",os.getOrderDate().getDate());//获取年月日中几号
                rsMap.put("number",os.getNumber());//可预约人数
                rsMap.put("reservations",os.getReservations());//已经预约人数
                mapList.add(rsMap);
            }
        }
        return mapList;
    }
}
